<?php
/**
 * 录音合成
 */
require_once dirname(__DIR__)."/login_filter.php";
use app\cls\tom\Response;
use app\cls\tom\SysResource;
use app\cls\tom\Tools;
use app\cls\sdk\FoundaoVideoSDK;
use app\cls\tom\FFmpeg;
use app\cls\tom\Config;
use app\model\Video;
use app\model\Music;
use app\model\UserVideo;
$my_uuid=$Auth["uuid"];
//接收参数
if(empty($_REQUEST["video_uuid"])  || empty($_REQUEST["audio_url"])){
    Response::output(-1,"参数错误");
}
$materialId = $_REQUEST["video_uuid"];//原始素材的视频的uuid
$audioUrl = $_REQUEST["audio_url"];//用户配音
$photo_frame =isset($_REQUEST["photo_frame"]) ? $_REQUEST["photo_frame"] :"";//用户选择的相框
//实例化数据库
$db = SysResource::getDB(true);
$videoModel = new Video($db);
//先去查询用户原始配音素材
$videoInfo = $videoModel->getInfo(["video_uuid"=>$materialId],"video_id,video_uuid,video_small_pic,cut_music_url,transcode_video_no_music_url,video_time,video_desc,video_join_type,video_join_sub,width,height");
if(empty($videoInfo)){
    Response::output(-1,"无效的请求");
}
//调用非编
$option1=[];
$option2=[];
$option3=[];
$FoundaoVideoSDK=new FoundaoVideoSDK();
$video_time=$videoInfo["video_time"];
//设置视频参数

$FoundaoVideo = $FoundaoVideoSDK->createInputVideo($videoInfo["transcode_video_no_music_url"],$option1);
$Foundao['video']=$FoundaoVideo;
//设置音频参数
$option2['duration']=$video_time*1000;
$FoundaoAudio = $FoundaoVideoSDK->createInputAudio($audioUrl,$option2);
$Foundao['audio']=$FoundaoAudio;
if(!empty($photo_frame)){
    //配音视频的宽高
    $width=$videoInfo["width"];
    $height=$videoInfo["height"];
    if(empty($width) || empty($height)){
        $ffmpeg=new FFmpeg();
        $wh=$ffmpeg->getVideoDetail($videoInfo["transcode_video_no_music_url"]);
        $width=$wh["width"];
        $height=$wh["height"];
    }
    $option3['width']=$width;
    $option3['height']=$height;
    $option3['img_url']=$photo_frame;
    $FoundaoLayer = $FoundaoVideoSDK->createInputLayer("img",0,0,0,$video_time*1000,$option3);
    $Foundao['layer']=$FoundaoLayer;
}

//设置输出路径
$path=upload_dir("video","mp4");
$option4=[];
$option4['width']=$videoInfo["width"];
$option4['height']=$videoInfo["height"];

$ret1 = $FoundaoVideoSDK->createOutput(trim($path['savename'],"/"), "local", "video",$option4);
$Foundao['createOutput']=$ret1;
if (empty($ret1)) {
    Response::output(-1,$FoundaoVideoSDK->getError());
}
//提交给彬总接口处理
$job_id = $FoundaoVideoSDK->submitJob();
if(empty($job_id)){
    Response::output(-1,"提交失败，请重新提交一次",$_SERVER['SERVER_ADDR']);
}
//添加数据到数据库
$data['uuid']=$my_uuid;
$data['material_id']=$job_id;
$data['video_uuid']=$videoInfo["video_uuid"];
$data['nick_name']=$Auth["nick_name"];
$data['nick_pic']=$Auth["nick_pic"];
$data['pic']=$videoInfo["video_small_pic"];
$data['video_desc']=$videoInfo["video_desc"];
$data['duration']=$videoInfo["video_time"];
$data['join_type']=$videoInfo["video_join_type"];
$data['join_type_sub']=$videoInfo["video_join_sub"];
$data['no_music_video_url']=$videoInfo["transcode_video_no_music_url"];
$data['music']=$videoInfo["cut_music_url"];
$data['width2']=$videoInfo["width"];
$data['height2']=$videoInfo["height"];
$data['first_pic3']=$videoInfo["first_pic1"];
$data['audio_url']=$audioUrl;
$data['add_time']=date("Y-m-d H:i:s");
$data['is_save']=1;
$data['video_url']=$path['savehttp'];
$db = SysResource::getDB(true);
$UserVideo=new UserVideo($db);
$res=$UserVideo->add_material($data);
if($res){
    $dd['job_id']=$job_id;
    $dd['move_name']=$path['movename'];
    $dd['video_url']=$path['savehttp'];
    Response::output(0,"添加成功2",$dd,$_SERVER['SERVER_ADDR']);
}else{
    Response::output(-1,"添加失败");
}








//获取毫秒级时间
function getMsecTime()
{
    list($msec, $sec) = explode(' ', microtime());
    $msectime =  (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
    return $msectime;
}
//获取上传目录
function upload_dir($type="tmp",$suffix="jpg"){
    //解决跨域问题
    $allUploadConfig = Config::load("cos_upload");

    $uploadConfig = $allUploadConfig[$type];

    //创建上传目录，配置上传
    $date1=date("Y",time());
    $date2=date("m",time());
    $date3=date("d",time());
    $dir="/".$date1."/".$date2."/".$date3;
    $uploadpath1=$uploadConfig['savepath'];
    $uploadpath2=$uploadConfig['savehttp'];
    $key1=rand(1000,10000);
    $key2=rand(10000,20000);
    $key3=rand();
    $file_name=$key1.$key2.$key3;
    //这是服务器上的上传地址
    $uploadpath['savepath']=$uploadpath1.$dir."/".$file_name.".{$suffix}";
    $uploadpath['savename']=$dir."/".$file_name.".{$suffix}";
    $uploadpath['movename']=$uploadpath1.$dir;
    if($type=="video"){
        mkdir($uploadpath1.$dir."/".$file_name,0777,true);
    }
    //这是网络访问地址
    $uploadpath['savehttp']=$uploadpath2.$dir."/".$file_name.".{$suffix}";
    return $uploadpath;
}








//try{
//    $material_id = Tools::UUID();//这是生成任务id
//    $db->insert("user_material",[
//        "uuid"=>$my_uuid,//用户uuid
//        "material_id"=>$material_id,//任务id
//        "video_uuid"=>$materialId,//原始素材uuid
//        "nick_name"=>$Auth["nick_name"],//用户呢称
//        "nick_pic"=>$Auth["nick_pic"],
//        "pic"=>$videoInfo["video_small_pic"],
//        "video_desc"=>$videoInfo["video_desc"],
//        "duration"=>$videoInfo["video_time"],
//        "join_type"=>$videoInfo["video_join_type"],
//        "join_type_sub"=>$videoInfo["video_join_sub"],
//        "no_music_video_url"=>$videoInfo["transcode_video_no_music_url"],
//        "music"=>$videoInfo["cut_music_url"],
//        "width2"=>$videoInfo["width"],
//        "height2"=>$videoInfo["height"],
//        "first_pic3"=>$videoInfo["first_pic1"],
//        "audio_url"=>$audioUrl,
//        "add_time"=>date("Y-m-d H:i:s"),
//        "is_save"=>1,
//    ]);
//    $db->update("video",["dub_num"=>"!dub_num+1"],["video_uuid"=>$materialId]);
//
//    $redis = SysResource::getRedis();
//    $dyKeyPrefix = REDIS_PREFIX."material_dy_";
//    $dyKey = $dyKeyPrefix.$materialId;
//    $redis->zAdd($dyKey,intval(microtime(true)*1000),$Auth["user_uuid"]);
//    //动态截断
//    $redis->zRemRangeByRank($dyKey,10,-1);
//    Response::output(0,"提交成功",["job_id"=>$material_id]);
//}catch (Exception $e){
//    Response::output(-1,"提交失败,请重试");
//}







